//
//  IMcmd.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/4.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class IMcmd: NSObject {
    
    static let shared = IMcmd()
    
    ///////////// 通用权利 //////////////
   
    //举手
    func handUp(isok:Bool,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = isok ? mt_wss_raiseHand:mt_wss_downHand
        let mesg = mode.toJSON()!.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    func modifyName(name:String,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = mt_wss_modifyName
        mode.username = name
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

     //弹幕
    func barrage(txt:String,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId) else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = mt_wss_barrage
        mode.txt = txt
        mode.username = Td.sh().lc.nickName
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //字幕
    func realtime(txt:String,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = mt_wss_qcloud
        mode.txt = txt
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //观众
    func audience(isEnter:Bool,userName:String?=nil,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.username = userName
        mode.cmd = isEnter ? mt_wss_enter_audience:mt_wss_exit_audience
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //观众指定观众
    func toAudience(toUserId:String,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.touserid = toUserId
        mode.username = Td.sh().name
        mode.cmd = mt_wss_enter_toAudience
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }
    
    //指向性通知
    func sendMessage(toUid:String?,text:String?,groupId:String?=nil) {
        guard let uid = Td.sh().params.userId,let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.roomid = gid
        mode.userid = uid
        mode.touserid = toUid
        mode.username = Td.sh().name
        mode.cmd = mt_wss_message
        mode.txt = text
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //2次请求上麦
    func send2UpMic(userId:String?=nil,groupId:String?=nil) {
      guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
          return
      }
      let mode = WebSocketMode()
      mode.userid = uid
      mode.roomid = gid
      mode.cmd = mt_wss_send2UpMic
      let mesg = mode.toJSON()?.toJsonString()
      IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }
    
    //2次确认上麦
    func accept2UpMic(toUserId:String,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
          return
        }
        let mode = WebSocketMode()
        mode.touserid = toUserId
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = mt_wss_acceptUpmic
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }
    
    //请求上麦
    func upMic(isok:Bool,userId:String?=nil,groupId:String?=nil) {
      guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
          return
      }
      let mode = WebSocketMode()
      mode.userid = uid
      mode.roomid = gid
      mode.cmd = isok ? mt_wss_reqUpmic:mt_wss_reqDownmic
      let mesg = mode.toJSON()?.toJsonString()
      IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }


    /////////////  主持人权利  ///////////

    //主持人弹幕
    func disDanmu(isok:Bool,userId:String?=nil,groupId:String?=nil) {
      guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
          return
      }
      let mode = WebSocketMode()
      mode.userid = uid
      mode.roomid = gid
      mode.cmd = isok ? mt_wss_host_disDanmu:mt_wss_host_danmu
      let mesg = mode.toJSON()?.toJsonString()
      IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //主持人
    func setRole(isAnchor:Bool,confirm:Int,userId:String?,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ??  Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.confirm = confirm
        mode.cmd = isAnchor ? mt_wss_host_swAnchor:mt_wss_host_swAudience
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }
     
    //是否全体静音
    func allMute(isok:Bool,userId:String?=nil,groupId:String?=nil) {
       guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ??  Td.sh().params.roomId)  else {
           return
       }
       let mode = WebSocketMode()
       mode.userid = uid
       mode.roomid = gid
       mode.cmd = isok ? mt_cmd_host_muteAll:mt_cmd_host_unmuteAll
       let mesg = mode.toJSON()?.toJsonString()
       IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //是否允许上麦
    func notAllowToRole(isok:Bool,userId:String?=nil,groupId:String?=nil) {
      guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
          return
      }
      let mode = WebSocketMode()
      mode.userid = uid
      mode.roomid = gid
      mode.cmd = isok ? mt_wss_host_notAllowToRole:mt_wss_host_allowToRole
      let mesg = mode.toJSON()?.toJsonString()
      IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //踢人
    func kick(userId:String,groupId:String?=nil) {
       guard let gid = (groupId ?? Td.sh().params.roomId)  else {
           return
       }
       let mode = WebSocketMode()
       mode.userid = userId
       mode.roomid = gid
       mode.cmd = mt_wss_host_removeUser
       let mesg = mode.toJSON()?.toJsonString()
       IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //关闭房间
    func exit(userId:String?=nil,groupId:String?=nil,succ:(()->())?=nil) {
        guard let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = userId
        mode.roomid = gid
        mode.cmd = mt_wss_host_exit
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid, succ:succ)
    }

    //主持人解除举手
    func downAllHand(userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = mt_wss_host_downAllHand
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //主持人解除某用户举手
    func downUserHand(userId:String,groupId:String?=nil) {
        guard let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = userId
        mode.roomid = gid
        mode.cmd = mt_wss_host_downUserHand
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }

    //设定焦点
    func focusUser(userId:String,groupId:String?=nil) {
       guard let gid = (groupId ?? Td.sh().params.roomId)  else {
           return
       }
       let mode = WebSocketMode()
       mode.userid = userId
       mode.roomid = gid
       mode.cmd = mt_wss_host_focuseUser
       let mesg = mode.toJSON()?.toJsonString()
       IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }
    
    //设定共享
    func sd(allow:Bool,userId:String?=nil,groupId:String?=nil) {
        guard let uid = (userId ?? Td.sh().params.userId),let gid = (groupId ?? Td.sh().params.roomId)  else {
            return
        }
        let mode = WebSocketMode()
        mode.userid = uid
        mode.roomid = gid
        mode.cmd = allow ? mt_wss_host_allowShare:mt_wss_host_stopShare
        let mesg = mode.toJSON()?.toJsonString()
        IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }



    //是否用户静音
    func muteUser(userId:String,isok:Bool,groupId:String?=nil) {
       guard let gid = (groupId ?? Td.sh().params.roomId)  else {
           return
       }
       let mode = WebSocketMode()
       mode.userid = userId
       mode.roomid = gid
       mode.cmd = isok ? mt_wss_host_muteUser:mt_wss_host_unMuteUser
       let mesg = mode.toJSON()?.toJsonString()
       IMEngine.shared.send(t: mesg ?? "", groupId: gid)
    }
    
}
